import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-11-11
 * Time: 14:47
 * To BE A Better Person-2020-3-2
 */
class Message{
    public String from;
    public String to;
    public String message;

    @Override
    public String toString() {
        return "Message{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}
@WebServlet("/message")
public class MessageServlet extends HttpServlet {

   // private ArrayList<Message> list=new ArrayList<>();
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
       // String s=objectMapper.writeValueAsString(list);
        ArrayList<Message> list= null;
        try {
            list = load();
            String respString = objectMapper.writeValueAsString(list);
            resp.getWriter().write(respString);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.将body中的json解析出来
        Message message=objectMapper.readValue(req.getInputStream(),Message.class);
        //将对象存到一个list中
        //list.add(message);

        try {
            save(message);
            //返回保存成功的响应
            System.out.println("message:"+message.toString());
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write("{ \"ok\": 1 }");
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    private void save(Message message) throws SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        try{

            connection=JDBCmess.getConnection();
            String sql="insert into message values (?,?,?);";
            statement=connection.prepareStatement(sql);
            statement.setString(1,message.from);
            statement.setString(2,message.to);
            statement.setString(3,message.message);
            int ret=statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCmess.close(connection,statement,null);
        }
    }
    private ArrayList<Message> load() throws SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        ArrayList<Message> list=new ArrayList<>();
        Message message=new Message();
        try {
            String sql="select * from message";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while(resultSet.next()){
                message.from=resultSet.getString("from");
                message.to=resultSet.getString("to");
                message.message=resultSet.getString("message");
                list.add(message);
            }
            //hhh
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCmess.close(connection,statement,resultSet);
        }


        return list;

    }



}
